package com.inclass.security;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cas.CasRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.CollectionUtils;

public class MyCasRealm extends CasRealm{
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String)principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        return authorizationInfo;
    }
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
    		AuthenticationToken casToken) throws AuthenticationException {
    	HDUCASToken token = (HDUCASToken) casToken;
        String ticket = (String)casToken.getCredentials();
//        ServiceValidator  serviceValidator = ValidatorFactory.factory(false).getServiceValidator();
//        serviceValidator.setServiceTicket(ticket);
//        serviceValidator.setCasValidateUrl(getCasService());
//        serviceValidator.validate();
//        System.out.println(serviceValidator.isAuthenticationSuccesful());
//        try {
//        	CASReceipt receipt = CASReceipt.getReceipt(serviceValidator);
//        	Map<String, String> map = receipt.getUserMap();
//            List<Object> principals = CollectionUtils.asList(casToken.getPrincipal());
//            PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
//            return new SimpleAuthenticationInfo(principalCollection, ticket);
//
//		} catch (CASAuthenticationException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
/*
 *           CasToken casToken = (CasToken) token;
        if (token == null) {
            return null;
        }
        
        String ticket = (String)casToken.getCredentials();
        if (!StringUtils.hasText(ticket)) {
            return null;
        }
        
        TicketValidator ticketValidator = ensureTicketValidator();

        try {
            // contact CAS server to validate service ticket
            Assertion casAssertion = ticketValidator.validate(ticket, getCasService());
            // get principal, user id and attributes
            AttributePrincipal casPrincipal = casAssertion.getPrincipal();
            String userId = casPrincipal.getName();
            log.debug("Validate ticket : {} in CAS server : {} to retrieve user : {}", new Object[]{
                    ticket, getCasServerUrlPrefix(), userId
            });

            Map<String, Object> attributes = casPrincipal.getAttributes();
            // refresh authentication token (user id + remember me)
            casToken.setUserId(userId);
            String rememberMeAttributeName = getRememberMeAttributeName();
            String rememberMeStringValue = (String)attributes.get(rememberMeAttributeName);
            boolean isRemembered = rememberMeStringValue != null && Boolean.parseBoolean(rememberMeStringValue);
            if (isRemembered) {
                casToken.setRememberMe(true);
            }
            // create simple authentication info
            List<Object> principals = CollectionUtils.asList(userId, attributes);
            PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
            return new SimpleAuthenticationInfo(principalCollection, ticket);
        } catch (TicketValidationException e) { 
            throw new CasAuthenticationException("Unable to validate ticket [" + ticket + "]", e);
        }

 */

        PrincipalCollection principalCollection = new SimplePrincipalCollection(token.getUserMap(), getName());
        return new SimpleAuthenticationInfo(principalCollection, ticket);

//    	return super.doGetAuthenticationInfo(token);
    }


}
